package com.accesshollywood.xmas2011.core;

import static playn.core.PlayN.assetManager;
import static playn.core.PlayN.graphics;

import org.jbox2d.common.IViewportTransform;
import org.jbox2d.common.OBBViewportTransform;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import playn.core.Image;
import playn.core.ImageLayer;

public class Billboard {
	public String		name;
	public Body			parentBody;
	private Image		skinImage;
	private ImageLayer	skin;
	public int			maxHitPoints;	// negative hitpoints means unbreakable
	public int			currHitPoints;	// curr hitpoints - obviously it will
										// never be greater than maxHitPoints

	public Billboard(String imageFileName) {
		this.name = imageFileName;
		this.parentBody = parentBody;
		applySkin(imageFileName);
	}

	public void update() {
		IViewportTransform viewportTransform = new OBBViewportTransform();
		viewportTransform.setYFlip(true);
		Vec2 worldVec = parentBody.getWorldCenter();
		Vec2 screenVec = new Vec2();
		viewportTransform.getWorldToScreen(worldVec, screenVec);
		screenVec = screenVec.mul(Master.worldScale);
		skin.setRotation(-parentBody.getAngle());
		skin.setTranslation(screenVec.x, screenVec.y);
	}

	public void applySkin(String imageFileName) {
		skinImage = assetManager().getImage(imageFileName);
		skin = graphics().createImageLayer(skinImage);
		skin.setOrigin(skinImage.width() / 2.0f, skinImage.height() / 2.0f);
		graphics().rootLayer().add(skin);
	}

	public boolean isBreakable() {
		if (maxHitPoints < 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean isBroken(){
		if(isBreakable() && currHitPoints <= 0){
			return true;
		}else{
			return false;
		}
	}
}
